<template>
  <div class="examroom_box">
    <el-table :data="tableData" style="width: 100%" stripe>
      <el-table-column
        label="序号"
        type="index"
        :index="tableIndex"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="上传人" show-overflow-tooltip>
        <template slot-scope="scope">
          <div v-if="scope.row.uploader != null" class="userInfo">
            <el-avatar :src="scope.row.uploader.avatar" :size="64"></el-avatar>
            <span>{{ scope.row.uploader.nickname }}</span>
          </div>
          <div v-else>暂不可查</div>
        </template>
      </el-table-column>
      <el-table-column label="房屋标题" prop="title" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="图片展示" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-image
            :src="scope.row.imageList[0].picture"
            class="roomImage"
            :preview-src-list="getImageArr(scope.row.imageList)"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="月租" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.price | priceFilter }}
        </template>
      </el-table-column>
      <el-table-column label="上传时间" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.modifyTime | timeFilter }}
        </template>
      </el-table-column>
      <el-table-column label="操作" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button type="primary" @click="permit(scope)">通过</el-button>
          <el-button type="danger" @click="refuse(scope)">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      background
      hide-on-single-page
      class="pagination"
      :page-size="size"
      :current-page="current"
      :total="total"
      @prev-click="getTableData"
      @next-click="getTableData"
      @current-change="getTableData"
    >
    </el-pagination>
  </div>
</template>

<script>
import { Loading } from "element-ui";
import axios from "@/request";
export default {
  name: "ExamRoom",
  data() {
    return {
      tableData: [],
      current: 1,
      size: 5,
      total: 100,
    };
  },
  mounted() {
    this.getTableData(1);
  },
  methods: {
    printRow(row) {
      console.log(row);
    },
    tableIndex(index) {
      return (this.current - 1) * this.size + index + 1;
    },
    getTableData(current) {
      let loading = Loading.service({
        text: "正在加载...",
        target: ".examroom_box",
      });
      axios
        .get("/room/getUncheckRoomList", {
          params: {
            current: current,
            size: this.size,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.total = res.data.data.total;
            this.tableData = res.data.data.records;
          } else {
            this.$message.error(res.data.data);
          }
          loading.close();
        })
        .catch((e) => {});
    },
    getImageArr(imageObjectArr) {
      let result = [];
      for (const imageObj of imageObjectArr) {
        result.push(imageObj.picture);
      }
      return result;
    },
    updateRoomState(roomId, code, index) {
      let loading = Loading.service({
        text: "正在加载...",
        target: ".examuser_box",
      });
      axios
        .get("/room/examUncheckRoom", {
          params: {
            roomId: roomId,
            code: code,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success(res.data.data);
            if (this.current > 0 && index == 0) {
              this.getTableData(this.current - 1);
            } else {
              this.getTableData(this.current);
            }
            loading.close();
          } else {
            this.$message.error(res.data.data);
            loading.close();
          }
        })
        .catch((e) => {
          loading.close();
        });
    },
    permit(scope) {
      this.updateRoomState(scope.row.id, 1, scope.$index);
    },
    refuse(scope) {
      this.updateRoomState(scope.row.id, 0, scope.$index);
    },
  },
};
</script>

<style scoped>
.userInfo {
  display: inline-flex;
  align-items: center;
  flex-direction: column;
}

.userInfo > span {
  display: block;
}

.roomImage >>> .el-image__inner {
  max-height: 100px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>